package net.goutalk.glcs.module.bi.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.bi.dto.AddVisualCategoryDto;
import net.goutalk.glcs.module.bi.dto.UpdateVisualCategoryDto;
import net.goutalk.glcs.module.bi.entity.VisualCategory;
import net.goutalk.glcs.module.bi.service.IVisualCategoryService;
import net.goutalk.glcs.module.bi.vo.VisualCategoryListVo;
import net.goutalk.glcs.module.bi.vo.VisualCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * BI 大屏分类 前端控制器
 * </p>
 *
 * @author zjq
 * @since 2022-09-21
 */
@RestController
@RequestMapping(GlobalConstant.BI_MODULE_PREFIX + "/category")
@Api(value = GlobalConstant.BI_MODULE_PREFIX + "/category", tags = "B分类大屏设计")
@AllArgsConstructor
public class VisualCategoryController {
    private final IVisualCategoryService visualCategoryService;


    @GetMapping("/list")
    @ApiOperation("查询BI分类(不分页)")
    public R list() {
        List<VisualCategory> list = visualCategoryService.list(Wrappers.lambdaQuery(VisualCategory.class)
                .select(VisualCategory.class, x -> VoToColumnUtil.fieldsToColumns(VisualCategoryListVo.class).contains(x.getProperty())));
        List<VisualCategoryListVo> visualCategoryListVos = BeanUtil.copyToList(list, VisualCategoryListVo.class);
        return R.ok(visualCategoryListVos);
    }


    @GetMapping(value = "/info")
    @ApiOperation("根据id查询分类详情")
    public R info(@RequestParam("id") Long id) {
        VisualCategory visualCategory = visualCategoryService.getById(id);
        if (visualCategory == null) {
            return R.error("查不到此分类信息");
        }
        return R.ok(BeanUtil.toBean(visualCategory, VisualCategoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增分类")
    public R add(@Valid @RequestBody AddVisualCategoryDto dto) {
        VisualCategory visualCategory = BeanUtil.toBean(dto, VisualCategory.class);
        // visualCategory.setIsDeleted(0);
        return R.ok(visualCategoryService.save(visualCategory));
    }


    @PutMapping
    @ApiOperation(value = "更新分类")
    public R update(@Valid @RequestBody UpdateVisualCategoryDto dto) {
        VisualCategory visualCategory = BeanUtil.toBean(dto, VisualCategory.class);
        visualCategory.setId(dto.getId());
        return R.ok(visualCategoryService.updateById(visualCategory));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(visualCategoryService.removeBatchByIds(ids));
    }
}



